package gov.kqqzyyy.monkey.ydhl.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.ArrayMap;
import android.view.View;
import android.widget.ExpandableListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import adapter.JCJGListAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import gov.kqqzyyy.monkey.ydhl.R;
import mytools.MyBaseActivity;
import mytools.MyServiceUtil;
import objects.JCJGDetail;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReportJCActivity extends MyBaseActivity implements SwipeRefreshLayout.OnRefreshListener, ExpandableListView.OnGroupClickListener {


    @BindView(R.id.jc_swipe_ly)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.jc_expandable_lv)
    ExpandableListView expandableListView;

    private List<JCJGDetail> mRISList;
    private List<JCJGDetail> mUISList;
    private List<JCJGDetail> mECGList;
    private List<JCJGDetail> mESList;
    private List<JCJGDetail> mPISList;

    private Call<String> risCall;
    private Call<String> uisCall;
    private Call<String> ecgCall;
    private Call<String> esCall;
    private Call<String> pisCall;

    private JCJGListAdapter mJCAdapter;

    private String[] parentsArray;
    private ArrayMap<String, List<JCJGDetail>> mJCDataSet;

    private boolean[] isTaskExecuting = {false, false, false, false, false};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_jc);
        ButterKnife.bind(this);

        mJCAdapter = new JCJGListAdapter(this);
        expandableListView.setAdapter(mJCAdapter);
        expandableListView.setOnGroupClickListener(this);

        swipeRefreshLayout.setColorSchemeResources(R.color.holo_green_dark, R.color.holo_green_light, R.color.holo_orange_light, R.color.holo_red_light);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        parentsArray = getResources().getStringArray(R.array.pacs_type);
        doFlushData(true);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (risCall != null && risCall.isExecuted()) {
            risCall.cancel();
        }
        if (uisCall != null && uisCall.isExecuted()) {
            uisCall.cancel();
        }
        if (ecgCall != null && ecgCall.isExecuted()) {
            ecgCall.cancel();
        }
        if (esCall != null && esCall.isExecuted()) {
            esCall.cancel();
        }
        if (pisCall != null && pisCall.isExecuted()) {
            pisCall.cancel();
        }
    }

    @Override
    public void onRefresh() {
        doFlushData(false);
    }

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//        if (expandableListView.getId() == R.id.jc_expandable_lv) {
//            if (mJCJGResults[position] == null || mJCJGResults[position].equals(""))
//                Toast.makeText(ReportJCActivity.this, parentsArray[position] + " 报告项目为空！", Toast.LENGTH_SHORT).show();
//        }
        return false;
    }

    private void doGetRis(String zyhm) {
        risCall = MyServiceUtil.getRisCall(zyhm);
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                isTaskExecuting[0] = false;
                doCancelAfterAll();
                String result = response.body();
                if (result != null) {
                    Gson gson = new Gson();
                    mRISList = gson.fromJson(result, new TypeToken<List<JCJGDetail>>() {
                    }.getType());
                    if (mRISList != null) {
                        mJCDataSet.put(parentsArray[0], mRISList);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                isTaskExecuting[0] = false;
                doCancelAfterAll();
            }
        };
        risCall.enqueue(callback);
    }


    private void doGetUis(String zyhm) {
        uisCall = MyServiceUtil.getUisCall(zyhm);
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                isTaskExecuting[1] = false;
                doCancelAfterAll();
                String result = response.body();
                if (result != null) {
                    Gson gson = new Gson();
                    mUISList = gson.fromJson(result, new TypeToken<List<JCJGDetail>>() {
                    }.getType());
                    if (mUISList != null) {
                        mJCDataSet.put(parentsArray[1], mUISList);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                isTaskExecuting[1] = false;
                doCancelAfterAll();
            }
        };
        uisCall.enqueue(callback);
    }


    private void doGetEcg(String zyhm) {
        ecgCall = MyServiceUtil.getEcgCall(zyhm);
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                isTaskExecuting[2] = false;
                doCancelAfterAll();
                String result = response.body();
                if (result != null) {
                    Gson gson = new Gson();
                    mECGList = gson.fromJson(result, new TypeToken<List<JCJGDetail>>() {
                    }.getType());
                    if (mECGList != null) {
                        mJCDataSet.put(parentsArray[2], mECGList);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                isTaskExecuting[2] = false;
                doCancelAfterAll();
            }
        };
        ecgCall.enqueue(callback);
    }


    private void doGetEs(String zyhm) {
        esCall = MyServiceUtil.getEsCall(zyhm);
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                isTaskExecuting[3] = false;
                doCancelAfterAll();
                String result = response.body();
                if (result != null) {
                    Gson gson = new Gson();
                    mESList = gson.fromJson(result, new TypeToken<List<JCJGDetail>>() {
                    }.getType());

                    if (mESList != null) {
                        mJCDataSet.put(parentsArray[3], mESList);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                isTaskExecuting[3] = false;
                doCancelAfterAll();
            }
        };
        esCall.enqueue(callback);
    }


    private void doGetPis(String zyhm) {
        pisCall = MyServiceUtil.getPisCall(zyhm);
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                isTaskExecuting[4] = false;
                doCancelAfterAll();
                String result = response.body();
                if (result != null) {
                    Gson gson = new Gson();
                    mPISList = gson.fromJson(result, new TypeToken<List<JCJGDetail>>() {
                    }.getType());
                    if (mPISList != null) {
                        mJCDataSet.put(parentsArray[4], mPISList);
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                isTaskExecuting[4] = false;
                doCancelAfterAll();
            }
        };
        pisCall.enqueue(callback);
    }

    private void doCancelAfterAll() {
        for (Boolean isExecuting : isTaskExecuting
                ) {
            if (isExecuting) {
                return;
            }
        }
        cancelProgressDialog();
        stopRefreshing();
        mJCAdapter.setDataSet(mJCDataSet);
    }

    private void doFlushData(boolean showProgress) {
        if (showProgress) {
            showProgressDialog("获取数据中");
        }
        mJCDataSet = new ArrayMap<>();
        for (int i = 0; i < 5; i++) {
            isTaskExecuting[i] = true;
        }
        doGetRis("");
        doGetUis("");
        doGetEcg("");
        doGetEs("");
        doGetPis("");
    }

}
